package paytreasure.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.PropertyUtilsBean;
/**
 *	对象与Map转换
 * 
 * */

public class ObjAnalysis {
	
	/**
	 * 对象转Map
	 * */
	public static Map<String, String> ConvertObjToMap(Object obj) {
		Map<String, String> map = new HashMap<String, String>();
		if (obj == null) return null;
		// System.out.println(obj.getClass());
		// 获取f对象对应类中的所有属性域
		Field[] fields = obj.getClass().getDeclaredFields();
		for (int i = 0, len = fields.length; i < len; i++) {
			String varName = fields[i].getName();
			try {
				// 获取原来的访问控制权限
				boolean accessFlag = fields[i].isAccessible();
				// 修改访问控制权限
				fields[i].setAccessible(true);
				// 获取在对象f中属性fields[i]对应的对象中的变量
				Object o = fields[i].get(obj);
				if (o!=null)
					map.put(varName, o.toString());
				// System.out.println("传入的对象中包含一个如下的变量：" + varName + " = " + o);
				// 恢复访问控制权限
				fields[i].setAccessible(accessFlag);
			} catch (IllegalArgumentException ex) {
				ex.printStackTrace();
			} catch (IllegalAccessException ex) {
				ex.printStackTrace();
			}
		}
		return map;
	}
	
	
	/**
	 * map对象  根据bean转换对象
	 * 
	 * 特殊类型时会出现异常
	 *	建议使用BeanUtils.populate
	 * */
	public static Object convertObj(Class type, Map map)    
            throws IntrospectionException, IllegalAccessException,    
            InstantiationException, InvocationTargetException {    
        BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性    
        Object obj = type.newInstance(); // 创建 JavaBean 对象    
    
        // 给 JavaBean 对象的属性赋值    
        PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();    
        for (int i = 0; i< propertyDescriptors.length; i++) {    
            PropertyDescriptor descriptor = propertyDescriptors[i];    
            String propertyName = descriptor.getName();    
            if (map.containsKey(propertyName)) {    
                // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。    
                Object value = map.get(propertyName);    
                Object[] args = new Object[1];    
                args[0] = value;    
                descriptor.getWriteMethod().invoke(obj, args);    
            }    
        }    
        return obj;    
    }
	
	public static void transMap2Bean(Map<String, Object> map, Object obj) {    
        
        try {    
            DateTimeConverter dtConverter = new DateTimeConverter();  
            ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();  
            convertUtilsBean.deregister(Date.class);  
            convertUtilsBean.register(dtConverter, Date.class);  
            BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean,new PropertyUtilsBean());  
            beanUtilsBean.populate(obj, map);  
        } catch (Exception e) {    
             
        }    
        return;    
    } 
	
}
